package com.myapp.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.gizwits.gizwifisdk.api.GizWifiDevice;
import com.gizwits.gizwifisdk.api.GizWifiSDK;
import com.gizwits.gizwifisdk.enumration.GizUserAccountType;
import com.gizwits.gizwifisdk.enumration.GizWifiErrorCode;
import com.gizwits.gizwifisdk.listener.GizWifiSDKListener;
import com.myapp.R;
import com.myapp.utils.AppUtils;
import com.myapp.utils.ClickUtils;
import com.myapp.utils.PreferencesUtil;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.myapp.Activity.LoginActivity.LOGIN_NAME;

/**
 * author : YuFeng
 * e-mail : 923462992@qq.com
 * date   : 2019/8/1317:14
 * desc   :
 */
public class ForgetPasswordActivity extends BaseActivity {
    private static final String TAG = "ForgetPasswordActivity";

    @BindView(R.id.iv_login_confirm)
    ImageView ivLoginConfirm;
    @BindView(R.id.btn_forget_return)
    Button btnForgetReturn;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.et_input_code)
    EditText etInputCode;
    @BindView(R.id.et_input_password)
    EditText etInputPassword;
    @BindView(R.id.et_input_password_again)
    EditText etInputPasswordAgain;
    private String Phone;
    private String name;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
        GizWifiSDK.sharedInstance().setListener(mListener);
        name = PreferencesUtil.getInstance(getApplicationContext()).getString(LOGIN_NAME,null);
        Log.i(TAG, "onCreate: "+name);
        if (name!=null){
            etPhone.setText(name);
        } else {
            etPhone.setText("");
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick({R.id.iv_login_confirm, R.id.btn_forget_return, R.id.tv_get_code})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_login_confirm:
                if (ClickUtils.isFastDoubleClick()) {
                    return;
                }
                Phone = etPhone.getText().toString();
                //检查phone格式
                if (!AppUtils.checkPhoneNum(Phone)){
                    Toast.makeText(getApplicationContext(),"手机号格式不对",Toast.LENGTH_SHORT).show();;
                    return;
                }

                String inputCode = etInputCode.getText().toString();
                String inputPassword = etInputPassword.getText().toString();
                String againPassword = etInputPassword.getText().toString();
                if(inputPassword.equals(againPassword)) {
                   
                    // 调用SDK的邮箱号注册接口
                    GizWifiSDK.sharedInstance().resetPassword(Phone, inputPassword, inputCode, GizUserAccountType.GizUserPhone);
                } else {
                    Toast.makeText(getApplicationContext(),"两次密码不同",Toast.LENGTH_SHORT).show();
                }
                break;

            case R.id.btn_forget_return:
                if (ClickUtils.isFastDoubleClick()) {
                    return;
                }
                finish();
                break;

            case R.id.tv_get_code:
                sendVerifyCode();
                break;
        }
    }

    // 实现回调
    GizWifiSDKListener mListener = new GizWifiSDKListener() {
        @Override
        public void didRegisterUser(GizWifiErrorCode result, String uid, String token) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS) {
                Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_SHORT).show();
                // 注册成功
                finish();
            } else {
                Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_SHORT).show();
                // 注册失败
            }
        }
        @Override
        public void didRequestSendPhoneSMSCode(GizWifiErrorCode result, String token) {
            if (result == GizWifiErrorCode.GIZ_SDK_SUCCESS){
                Toast.makeText(getApplicationContext(),"发送验证码成功",Toast.LENGTH_SHORT).show();
                timer.start();
            } else {
                Toast.makeText(getApplicationContext(),"发送验证码失败",Toast.LENGTH_SHORT).show();
                tvGetCode.setEnabled(true);
            }
        }

    };

    private void sendVerifyCode() {
        Phone = etPhone.getText().toString();
        //检查phone格式
        if (!AppUtils.checkPhoneNum(Phone)){
            Toast.makeText(getApplicationContext(),"手机号格式不对",Toast.LENGTH_SHORT).show();;
            return;
        }
        Log.i(TAG, "sendVerifyCode: "+"到达"+" phone:"+Phone);

        GizWifiSDK.sharedInstance().requestSendPhoneSMSCode("4f5441985a3d4ac1802dd7ef92811b20",Phone);
        tvGetCode.setEnabled(false);
    }

    private CountDownTimer timer = new CountDownTimer(60000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            //doing something...
            String showTimeRemain = String.format(Locale.getDefault(),"%d秒后重发",(millisUntilFinished / 1000));
            tvGetCode.setText(showTimeRemain);
            tvGetCode.setEnabled(false);

        }
        @Override
        public void onFinish() {
            tvGetCode.setText("获得验证码");
            tvGetCode.setEnabled(true);

        }
    };


}
